package com.fire.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fire.bo.classIndex.req.ClassIndexReq;
import com.fire.bo.classIndex.req.GoodsClassReq;
import com.fire.bo.classIndex.req.GoodsReq;
import com.fire.bo.classIndex.req.SelClassIndexReq;
import com.fire.bo.classIndex.res.ClassGoodRes;
import com.fire.bo.classIndex.res.ClassIndexRes;
import com.fire.bo.coupon.req.DelCouponReq;
import com.fire.common.RspObjBean;
import com.fire.service.ClassIndexService;
import com.fire.util.BussinessUtils;
import com.fire.util.ErrRsp;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@CrossOrigin
@RequestMapping("/pc/classIndex")
@Api(value = "设置分类", description = "设置分类")
public class ClassIndexController
{
	@Autowired
	private ClassIndexService classIndexService;
	
	/**
     * 根据部门编号查询员工总数和高管人数
     * @param reqPersonEntryDateBean
     * @param bindingResult
     * @return
     * @version:v1.0
     * @author:ZhangPeng
     * @date:2017年06月02日 上午09:07:14
     */
    @ApiOperation(value = "分类新增", notes = "分类新增")
    @RequestMapping(value = "/addClassIndex", method = RequestMethod.POST)
    public @ResponseBody RspObjBean addClassIndex(@RequestBody @Valid ClassIndexReq beans, BindingResult bindingResult)
    {
        // 非空验证
        if (bindingResult.hasErrors())
        {
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE, bindingResult.getFieldError().getDefaultMessage());
        }

        try
        {
        	classIndexService.addClassIndex(beans);
            // 根据上级类别key得到类别集合
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE, ErrRsp.SUCCESS_CODE_DESC, null);
        }
        catch (Exception e)
        {
        	e.printStackTrace();
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE, ErrRsp.SYSTEM_ERROR_DESC);
        }
    }
    
    /**
     * 根据部门编号查询员工总数和高管人数
     * @param reqPersonEntryDateBean
     * @param bindingResult
     * @return
     * @version:v1.0
     * @author:ZhangPeng
     * @date:2017年06月02日 上午09:07:14
     */
    @ApiOperation(value = "分类修改", notes = "分类修改")
    @RequestMapping(value = "/updateClassIndex", method = RequestMethod.POST)
    public @ResponseBody RspObjBean updateClassIndex(@RequestBody @Valid ClassIndexRes bean, BindingResult bindingResult)
    {
        // 非空验证
        if (bindingResult.hasErrors())
        {
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE, bindingResult.getFieldError().getDefaultMessage());
        }

        try
        {
        	classIndexService.updateClassIndex(bean);
            // 根据上级类别key得到类别集合
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE, ErrRsp.SUCCESS_CODE_DESC, null);
        }
        catch (Exception e)
        {
        	e.printStackTrace();
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE, ErrRsp.SYSTEM_ERROR_DESC);
        }
    }
    
    /**
     * 根据部门编号查询员工总数和高管人数
     * @param reqPersonEntryDateBean
     * @param bindingResult
     * @return
     * @version:v1.0
     * @author:ZhangPeng
     * @date:2017年06月02日 上午09:07:14
     */
    @ApiOperation(value = "分类删除", notes = "分类删除")
    @RequestMapping(value = "/delClassIndex", method = RequestMethod.POST)
    public @ResponseBody RspObjBean delClassIndex(@RequestBody @Valid DelCouponReq info, BindingResult bindingResult)
    {
        // 非空验证
        if (bindingResult.hasErrors())
        {
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE, bindingResult.getFieldError().getDefaultMessage());
        }

        try
        {
        	classIndexService.delClassIndex(info);
            // 根据上级类别key得到类别集合
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE, ErrRsp.SUCCESS_CODE_DESC, null);
        }
        catch (Exception e)
        {
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE, ErrRsp.SYSTEM_ERROR_DESC);
        }
    }
    
    /**
     * 根据部门编号查询员工总数和高管人数
     * @param reqPersonEntryDateBean
     * @param bindingResult
     * @return
     * @version:v1.0
     * @author:ZhangPeng
     * @date:2017年06月02日 上午09:07:14
     */
    @ApiOperation(value = "分类列表查询", notes = "分类列表查询", response = ClassIndexRes.class)
    @RequestMapping(value = "/selClassIndex", method = RequestMethod.POST)
    public @ResponseBody RspObjBean selClassIndex(@RequestBody @Valid SelClassIndexReq bean, BindingResult bindingResult)
    {
        // 非空验证
        if (bindingResult.hasErrors())
        {
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE, bindingResult.getFieldError().getDefaultMessage());
        }

        try
        {
        	List<ClassIndexRes> list = classIndexService.selClassIndex(bean);
        	
            // 根据上级类别key得到类别集合
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE, ErrRsp.SUCCESS_CODE_DESC, list,10);
        }
        catch (Exception e)
        {
        	e.printStackTrace();
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE, ErrRsp.SYSTEM_ERROR_DESC);
        }
    }
    
    @ApiOperation(value = "分类列表查询单个", notes = "首页分类列表查询", response = ClassIndexRes.class)
    @RequestMapping(value = "/selClassIndexById", method = RequestMethod.POST)
    public @ResponseBody RspObjBean selClassIndexById(@RequestBody @Valid DelCouponReq bean, BindingResult bindingResult)
    {
        // 非空验证
        if (bindingResult.hasErrors())
        {
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE, bindingResult.getFieldError().getDefaultMessage());
        }

        try
        {
        	ClassIndexRes info = classIndexService.selClassIndexById(bean);
            // 根据上级类别key得到类别集合
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE, ErrRsp.SUCCESS_CODE_DESC, info);
        }
        catch (Exception e)
        {
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE, ErrRsp.SYSTEM_ERROR_DESC);
        }
    }
    
    @ApiOperation(value = "商品分类列表", notes = "商品分类列表", response = ClassGoodRes.class)
    @RequestMapping(value = "/findAllGood", method = RequestMethod.POST)
    public @ResponseBody RspObjBean findAllGood(@RequestBody @Valid GoodsReq bean, BindingResult bindingResult)
    {
        // 非空验证
        if (bindingResult.hasErrors())
        {
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE, bindingResult.getFieldError().getDefaultMessage());
        }

        try
        {
        	List<ClassGoodRes> info = classIndexService.findAllGood(bean);
        	int count = classIndexService.findAllGoodCount(bean);
            // 根据上级类别key得到类别集合
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE, ErrRsp.SUCCESS_CODE_DESC, info,count);
        }
        catch (Exception e)
        {
        	e.printStackTrace();
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE, ErrRsp.SYSTEM_ERROR_DESC);
        }
    }
    
    @ApiOperation(value = "商品分配类", notes = "商品分配类")
    @RequestMapping(value = "/insertClassGood", method = RequestMethod.POST)
    public @ResponseBody RspObjBean insertClassGood(@RequestBody @Valid List<GoodsClassReq> bean, BindingResult bindingResult)
    {
        // 非空验证
        if (bindingResult.hasErrors())
        {
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE, bindingResult.getFieldError().getDefaultMessage());
        }

        try
        {
        	classIndexService.insertClassGood(bean);
            // 根据上级类别key得到类别集合
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE, ErrRsp.SUCCESS_CODE_DESC, null);
        }
        catch (Exception e)
        {
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE, ErrRsp.SYSTEM_ERROR_DESC);
        }
    }
    
    @ApiOperation(value = "商品下所属分类", notes = "商品下所属分类",response = ClassIndexRes.class)
    @RequestMapping(value = "/findAllClass", method = RequestMethod.POST)
    public @ResponseBody RspObjBean findAllClass(@RequestBody @Valid DelCouponReq bean, BindingResult bindingResult)
    {
        // 非空验证
        if (bindingResult.hasErrors())
        {
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE, bindingResult.getFieldError().getDefaultMessage());
        }

        try
        {
        	Map<String, Object> map = new HashMap<>();
        	
        	map.put("id", bean.getId());
        	
        	List<ClassIndexRes> list = classIndexService.findAllClass(map);
            // 根据上级类别key得到类别集合
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE, ErrRsp.SUCCESS_CODE_DESC, list);
        }
        catch (Exception e)
        {
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE, ErrRsp.SYSTEM_ERROR_DESC);
        }
    }
}
